package info.maxhis.bn;

import info.maxhis.bn.birthday.AddBirthdayActivity;
import info.maxhis.bn.birthday.BirthdayListActivity;
import info.maxhis.bn.birthday.SearchBirthdayActivity;
import info.maxhis.bn.renren.RenrenFriendListActivity;
import info.maxhis.bn.sms.SMSListActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

/**
 * 主页面
 * 
 * @author iStar
 * 
 */
public class MainActivity extends BaseActivity {
	protected static final int DONE = 0;
	private ListView mainList;
	private List<Map<String, Object>> data;
	private Resources res;
	private Context context;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main_list);
		context = this;
		mainList = (ListView) findViewById(R.id.main_list);
		prepareData();
		SimpleAdapter adapter = new SimpleAdapter(this, data,
				R.layout.main_list_item, new String[] { "Icon", "Menu",
						"Detail" }, new int[] { R.id.menu_img, R.id.menu_title,
						R.id.menu_detail });
		mainList.setAdapter(adapter);
		mainList.setTextFilterEnabled(true);
		mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Intent intent = new Intent();
				switch (position) {
				case 0:
					intent.setClass(context, BirthdayListActivity.class);
					startActivity(intent);
//					finish();
					break;

				case 1:
					intent.setClass(context, AddBirthdayActivity.class);
					startActivity(intent);
					break;

				case 2:
					intent.setClass(context, SearchBirthdayActivity.class);
					startActivity(intent);
					break;
					
				case 3:
					intent.setClass(context, SMSListActivity.class);
					startActivity(intent);
					break;

				case 4:
					intent.setClass(context, RenrenFriendListActivity.class);
					startActivity(intent);
					finish();
					break;
				}
			}
		});
	}

	/**
	 * 显示菜单
	 */
	private void prepareData() {
		res = getResources();
		String[] arrMen = res.getStringArray(R.array.arrMenu);
		String[] arrMenudt = res.getStringArray(R.array.arrMenuDetail);
		data = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < arrMen.length; i++) {
			Map<String, Object> item = new HashMap<String, Object>();
			if (i == 0) {
				item.put("Menu", arrMen[i]);
				item.put("Icon", R.drawable.view);
			} else if (i == 1) {
				item.put("Menu", arrMen[i]);
				item.put("Icon", R.drawable.add);
			} else if (i == 2) {
				item.put("Menu", arrMen[i]);
				item.put("Icon", R.drawable.search);
			} else if (i == 3) {
				item.put("Menu", arrMen[i]);
				item.put("Icon", R.drawable.setting);
			} else if (i == 4) {
				item.put("Menu", arrMen[i]);
				item.put("Icon", R.drawable.renren);
			}
			item.put("Detail", arrMenudt[i]);
			data.add(item);
		}
	}
/*	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode ==  KeyEvent.KEYCODE_BACK) {
			Intent mHomeIntent = new Intent(Intent.ACTION_MAIN, null);
			mHomeIntent.addCategory(Intent.CATEGORY_HOME);
			mHomeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(mHomeIntent);
		}
		return super.onKeyDown(keyCode, event);
	}
*/
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN
				&& event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			// 退出到主程序界面
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setMessage("您确定退出吗？")
					.setTitle("友情提示")
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {
									arg0.cancel();
								}
							})
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,int id) {
									MainActivity.this.finish();
								}
							});
			AlertDialog alert = builder.create();
			alert.show();
		}
		return false;
	};
}